package com.ss.android.article.browser.database.a;

import android.app.Application;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.a.a.s;
import com.ss.android.common.app.AbsApplication;

import java.util.ArrayList;
import java.util.List;

public class b extends SQLiteOpenHelper implements e {
    private static b a;
    @Nullable
    private SQLiteDatabase b;

    private b(@NonNull Application arg4) {
        super(((Context) arg4), "downloadManager", null, 1);
        arg4.getString(com.ss.android.article.browser.R.string.a1u);
    }

    public static b a() {
        if (com.ss.android.article.browser.database.a.b.a == null) {
            Class v1 = b.class;
            synchronized (v1) {
                if (com.ss.android.article.browser.database.a.b.a == null) {
                    com.ss.android.article.browser.database.a.b.a = new b(AbsApplication.getApplication());
                }
            }
        }

        return com.ss.android.article.browser.database.a.b.a;
    }

    static SQLiteDatabase a(b arg1) {
        return arg1.d();
    }

    static List a(Cursor arg3) {
        ArrayList v1 = new ArrayList();
        while (arg3.moveToNext()) {
            a v2 = new a();
            String v0 = arg3.getString(arg3.getColumnIndex("url"));
            if (v0 == null) {
                v0 = "";
            }

            v2.a = v0;
            v0 = arg3.getString(arg3.getColumnIndex("title"));
            if (v0 == null) {
                v0 = "";
            }

            v2.b = v0;
            v0 = arg3.getString(arg3.getColumnIndex("size"));
            if (v0 == null) {
                v0 = "";
            }

            v2.c = v0;
            ((List) v1).add(v2);
        }

        arg3.close();
        return ((List) v1);
    }

    @NonNull
    public final com.a.a.a b() {
        return com.a.a.a.a(new com.ss.android.article.browser.database.a.c(this));
    }

    @NonNull
    public final s c() {
        return s.a(new d(this));
    }

    @NonNull
    @WorkerThread
    private SQLiteDatabase d() {
        SQLiteDatabase v0_1;
        synchronized (this) {
            if (this.b == null || !this.b.isOpen()) {
                this.b = this.getWritableDatabase();
            }

            v0_1 = this.b;
        }
        return v0_1;
    }

    public void onCreate(@NonNull SQLiteDatabase arg3) {
        arg3.execSQL("CREATE TABLE " + DatabaseUtils.sqlEscapeString("download") + '(' + DatabaseUtils.sqlEscapeString("id") + " INTEGER PRIMARY KEY," + DatabaseUtils.sqlEscapeString("url") + " TEXT," + DatabaseUtils.sqlEscapeString("title") + " TEXT," + DatabaseUtils.sqlEscapeString("size") + " TEXT)");
    }

    public void onUpgrade(@NonNull SQLiteDatabase arg3, int arg4, int arg5) {
        arg3.execSQL("DROP TABLE IF EXISTS " + DatabaseUtils.sqlEscapeString("download"));
        this.onCreate(arg3);
    }
}

